package DataBase;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;

import DataObjects.FireForManageTableOrder;

public class SaveFires {
	
	private Vector<FireForManageTableOrder> fires;
	private int status=0;
	
	
	
	public SaveFires(Vector<FireForManageTableOrder> fires)
	{
		this.fires=fires;
	}
	
	
	
	public int save()
	{
		
SQLConnection sc=new SQLConnection();
		
		try {
			
			PreparedStatement ps1 = sc.getConnection().prepareStatement("INSERT INTO Fire(orderId,rid,rname,isFired,isSubmitted,timeFire,mid,extraName,extraRid,souceName,souceRid,comments,actionLevel,quantity) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);");
			PreparedStatement ps2 = sc.getConnection().prepareStatement("UPDATE order_from_a_table OFT SET OFT.isFired='1' WHERE OFT.orderId=? AND OFT.mid=? AND OFT.rid=? AND OFT.isFired='0' AND OFT.extraRid=? AND OFT.souceRid=? AND OFT.comments=? ORDER BY OFT.mid DESC LIMIT 1;");
			sc.getConnection().setAutoCommit(false);
			
			
			for(int i=0;i<fires.size();i++)
			{

				FireForManageTableOrder fire=fires.get(i);
				ps1.setInt(1,fire.getOrderId());
				ps1.setInt(2,fire.getRid());
				ps1.setString(3, fire.getNameOfRecipe());
				if(fire.isFired())
				{
					ps1.setInt(4, 1);
					ps1.setString(6, fire.getTimeOfFire());
				}
				else
				{
					ps1.setInt(4,0);
				}
				
				if(fire.isSubmitted())
				{
					ps1.setInt(5, 1);
				}
				else
				{
					ps1.setInt(5,0);
				}		
				ps1.setInt(7, fire.getMid());
				if(fire.getExtra()==null)
				{
					ps1.setNull(8, java.sql.Types.VARCHAR );
					ps1.setInt(9, 0);
				}
				else
				{
					ps1.setString(8, fire.getExtra().getName());
					ps1.setInt(9, Integer.parseInt(fire.getExtra().getId()));
				}
				
				if(fire.getSouce()==null)
				{
					ps1.setNull(10, java.sql.Types.VARCHAR );
					ps1.setInt(11, 0);
				}
				else
				{
				ps1.setString(10, fire.getSouce().getName());
				ps1.setInt(11, Integer.parseInt(fire.getSouce().getId()));
				}
				ps1.setString(12, fire.getComments());
				ps1.setString(13, fire.getActionLevel());
				ps1.setInt(14, fire.getQuantity());
				System.out.println("============================== proverka");
				System.out.println(fire.getQuantity());
				ps1.executeUpdate();				
				
				
				ps2.setInt(1, fire.getOrderId());
				ps2.setInt(2, fire.getMid());
				ps2.setInt(3, fire.getRid());
				
				if(fire.getExtra()==null)
				{
					ps2.setInt(4, 0);
				}
				else
				{
					ps2.setInt(4, Integer.parseInt(fire.getExtra().getId()));
				}
				
				if(fire.getSouce()==null)
				{
					ps2.setInt(5,0);
				}
				else
				{
					ps2.setInt(5,Integer.parseInt(fire.getSouce().getId()));
				}
				ps2.setString(6, fire.getComments());
				

				ps2.executeUpdate();
			}
			
			
			
			
			
			
			
			
			sc.getConnection().commit();
			 sc.getConnection().close();
			ps1.close();
			ps2.close();
			status=1;
		}
		
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			status=0;
		}
		
		
		
		return status;
	}
	
	

}
